//import apkeep.*;
//import com.alibaba.fastjson.JSON;
//import com.alibaba.fastjson.JSONObject;
//import com.alibaba.fastjson.JSONReader;
//import com.google.common.collect.Collections2;
//
//import java.io.*;
//import java.nio.ByteBuffer;
//import java.nio.ByteOrder;
//import java.util.*;
//
//public class Fb {
//    public static void test(String[] args) throws IOException {
//        String[] devicenames = {"atla", "chic", "hous", "kans", "losa", "newy32aoa", "salt", "seat", "wash"};
//        Collection<List<String>> pmts = Collections2.permutations(new ArrayList<>(Arrays.asList(devicenames)));
//        for (List<String> dns : pmts) {
//            test(dns.stream().toArray(String[]::new));
//        }
//    }
//
//    public static void main(String[] devicenames) throws IOException {
//        System.out.println("Good day!");
//        long start = System.nanoTime();
//
////        String[] devicenames = {"atla", "chic", "hous", "kans", "losa", "newy32aoa", "salt", "seat", "wash"};
//
//        Collection<List<String>> pmts = Collections2.permutations(new ArrayList<>(Arrays.asList(devicenames)));
//
//        Network n = new Network();
//
//        n.addDevice("default");
//
//        File inputFile = new File("fb/pnb_switches");
//        Scanner in = new Scanner(inputFile);
//        while (in.hasNextLine()) {
//            String linestr = in.nextLine();
//            String[] tokens = linestr.split(" ");
//            n.addDevice(tokens[0]);
//            n.addLink(tokens[0], "default", "default", tokens[0] + "-peer-default");
//        }
//
//        Model model = new Model();
//        n.model = model;
//
//        for (Device device : n.getDevices().values()) {
//            if (device.getName() == "default") {
//                continue;
//            }
//            Port defaultPort = device.getPortByName("default");
//            byte[] bb = {0, 0, 0, 0};
//            Rule rule = new Rule(bb, 0, defaultPort);
//            n.addRule(device, rule);
//            model.portToPreds.put(defaultPort, new HashSet<>());
//            model.portToPreds.get(defaultPort).add(1);
//            model.predToPorts.get(1).add(defaultPort);
//        }
//
//
//
////        LinkedList<Update> updates = new LinkedList<>();
////        String s = readJsonFile("/Users/gd/projects/carbide/fbdata/fbdata.txt");
//
//        File file = new File("/Users/gd/projects/carbide/fbdata/fbdata.txt");
//        InputStreamReader isr = null;
//        BufferedReader bufferedReader = null;
//        try {
//            isr = new InputStreamReader(new FileInputStream(file), "utf-8");
//            bufferedReader = new BufferedReader(isr);
//
//            JSONReader reader = new JSONReader(bufferedReader);
//            reader.startObject();
//            int i = 0;
//            while (reader.hasNext()) {
//                String key = reader.readString();
//                Device device = n.getDevice(key);
//                reader.startObject();
//                long ts = System.nanoTime();
//                while (reader.hasNext()) {
//                    String ipstr = reader.readString();
//                    String portname = reader.readObject().toString();
//                    Port p = device.getPortByName(portname);
//                    if (p == null) {
//                        p = device.addPortByName(portname);
//                    }
//                    long ip = Long.parseLong(ipstr);
//                    ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES).order(ByteOrder.nativeOrder());
//                    buffer.putLong((int) ip);
//                    Rule rule = new Rule(buffer.array(), 16, p);
////                    Update update = new Update(device, rule);
////                    updates.add(update);
//                    n.addRule(device, rule);
//                }
//                reader.endObject();
//                i++;
//                System.out.println(i);
//                System.out.println(System.nanoTime() - ts);
//            }
//            reader.endObject();
//        } catch (UnsupportedEncodingException e) {
//            e.printStackTrace();
//        } catch (FileNotFoundException e) {
//            e.printStackTrace();
//        } finally {
//            try {
//                if (null != isr) {
//                    isr.close();
//                }
//                if (null != bufferedReader) {
//                    bufferedReader.close();
//                }
//            } catch (Exception e2) {
//            }
//        }
//
////        JSONObject jobj = JSON.parseObject(s);
////        for (String dname : jobj.keySet()) {
////            JSONObject route = (JSONObject) jobj.get(dname);
////            for (String ipstr : route.keySet()) {
////                String portname = route.getString(ipstr);
////                Device device = n.getDevice(dname);
////                Port p = device.getPortByName(portname);
////                if (p == null) {
////                    p = device.addPortByName(portname);
////                }
////                long ip = Long.parseLong(ipstr);
////                ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES).order(ByteOrder.nativeOrder());
////                buffer.putLong((int) ip);
////                Rule rule = new Rule(buffer.array(), 16, p);
////                Update update = new Update(device, rule);
////                updates.add(update);
////            }
////
////        }
//
//
////        for (String name : devicenames) {
////            for (int i = updates.size(); i-- > 0;) {
////                if (updates.get(i).getDevice().getName().equals(name)) {
////                    updates.get(i).setLast(true);
////                    break;
////                }
////            }
////        }
//
////        long ip = 522018560;
////        ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES).order(ByteOrder.nativeOrder());
////        buffer.putLong((int)ip);
////        int hs = model.bddEngine.encodeIpv4(BitSet.valueOf(buffer.array()), 8);
////        HashSet<String> devnames = new HashSet<>(Arrays.asList("atla", "chic", "hous", "kans", "losa", "newy32aoa", "salt", "seat", "wash", "default"));
////        apkeep.automata.Automata a = apkeep.automata.Automata.fromString("atla,.*,wash,.*", devnames);
////        Property property = new Property(hs, "atla", a);
////        int i = 0;
//////        boolean ed = false;
////        for (Update update : updates) {
////            i++;
////            n.addRule(update.getDevice(), update.getRule());
//////            if (r == 3 || r == 4) {
//////                System.out.println("update idx: " + i + " " + r);
//////                writeFile1(String.valueOf(i) + " " + r);
//////                ed = true;
//////                break;
//////            }
////        }
////        if (!ed) {
////            writeFile1( "3600 " + 0);
////        }
//
//        System.out.println(model.predToPorts.size());
//        System.out.println((System.nanoTime() - start) / 1000000000);
//    }
//
//    public static void writeFile1(String str) throws IOException {
//        try {
//            String filename = "out-sf.txt";
//            FileWriter fw = new FileWriter(filename, true); //the true will append the new data
//            fw.write(str + "\n");//appends the string to the file
//            fw.close();
//        } catch (IOException ioe) {
//            System.err.println("IOException: " + ioe.getMessage());
//        }
//    }
//
//    public static String readJsonFile(String fileName) {
//        String jsonStr = "";
//        try {
//            File jsonFile = new File(fileName);
//            FileReader fileReader = new FileReader(jsonFile);
//            Reader reader = new InputStreamReader(new FileInputStream(jsonFile), "utf-8");
//            int ch = 0;
//            StringBuffer sb = new StringBuffer();
//            while ((ch = reader.read()) != -1) {
//                sb.append((char) ch);
//            }
//            fileReader.close();
//            reader.close();
//            jsonStr = sb.toString();
//            return jsonStr;
//        } catch (IOException e) {
//            e.printStackTrace();
//            return null;
//        }
//    }
//}
